package com.ambulance.project.biz.interceptor;

import com.alibaba.fastjson.JSON;
import com.ambulance.framework.web.domain.AjaxResult;
import com.ambulance.project.biz.entity.Doctor;
import com.ambulance.project.biz.entity.Nurse;
import com.ambulance.project.biz.enums.Role;
import com.ambulance.project.biz.mapper.MyCustomerMapper;
import com.ambulance.project.biz.service.DoctorService;
import com.ambulance.project.biz.service.NurseService;
import com.ambulance.utils.CommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/***
 *  @description
 *  @author zhao
 *  @date: 2020/4/15
 *  @version: v1.0
 */
@Component
public class MyPadValidInterceptor implements HandlerInterceptor {
    /**
     * preHandle在执行Controller之前执行
     * 返回true：继续执行处理器逻辑，包含Controller的功能
     * 返回false：中断请求
     * <p>
     * 处理器执行前方法
     */

    @Autowired
    private MyCustomerMapper myCustomerMapper;

    @Autowired
    private DoctorService doctorService;

    @Autowired
    private NurseService nurseService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        String requestURI = request.getRequestURI();
        if (requestURI.contains("login")) {
            return true;
        }
        String token = request.getHeader("token");
        String role = request.getHeader("role");
        String userMark = request.getHeader("userMark");

        if (CommonUtils.checkNull(token) || CommonUtils.checkNull(role)) {
            String jsonString = JSON.toJSONString(AjaxResult.error("校验参数为空"));
            ReturnJsonUtil.returnJson(response, jsonString);
            return false;
        }

        if(Role.DOCTOR.name().equals(role)){
            Doctor doctor = doctorService.getById(userMark);
            if(CommonUtils.checkNull(doctor)){
                String jsonString = JSON.toJSONString(AjaxResult.error("医生标识不正确"));
                ReturnJsonUtil.returnJson(response, jsonString);
                return false;
            }
        }else if(Role.NURSE.name().equals(role)){
            Nurse nurse = nurseService.getById(userMark);
            if(CommonUtils.checkNull(nurse)){
                String jsonString = JSON.toJSONString(AjaxResult.error("护士标识不正确"));
                ReturnJsonUtil.returnJson(response, jsonString);
                return false;
            }
        }else {
            String jsonString = JSON.toJSONString(AjaxResult.error("校验失败"));
            ReturnJsonUtil.returnJson(response, jsonString);
            return false;
        }


        // 通过token获取uid并重新放入header中
        Integer uid = myCustomerMapper.selectUserIDByToken(token);
        if (CommonUtils.checkNull(uid)) {
            String jsonString = JSON.toJSONString(AjaxResult.error("校验失败"));
            ReturnJsonUtil.returnJson(response, jsonString);
            return false;
        }
        return true;
    }

    /**
     * postHandle在请求执行完之后 渲染ModelAndView返回之前执行
     * <p>
     * 处理器处理后方法
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
                           ModelAndView modelAndView) throws Exception {
        String contextPath = request.getContextPath();
        String servletPath = request.getServletPath();
        String requestURI = request.getRequestURI();
        StringBuffer requestURL = request.getRequestURL();


        System.out.println("getContextPath:" + contextPath);
        System.out.println("getServletPath:" + servletPath);
        System.out.println("getRequestURI:" + requestURI);
        System.out.println("getRequestURL:" + requestURL);

        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);

        System.out.println("MyInterceptor-处理器处理后方法postHandle");
    }



    /**
     * afterCompletion在整个请求执行完毕后执行,无论是否发生异常都会执行
     * <p>
     * 处理器完成后方法
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {

        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
        System.out.println("MyInterceptor-处理器完成后方法afterCompletion");

    }

}
